package com.msb.mall.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;
import com.msb.mall.product.dao.AttrGroupDao;
import com.msb.mall.product.entity.AttrEntity;
import com.msb.mall.product.entity.AttrGroupEntity;
import com.msb.mall.product.entity.vo.AttrGroupWithAttrsVo;
import com.msb.mall.product.entity.vo.SpuItemGroupAttrVo;
import com.msb.mall.product.service.AttrGroupService;
import com.msb.mall.product.service.AttrService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询列表数据
     *      根据列表编号来查询
     * @param params 查询的其他条件
     * @param catId：//根据列表编号来查询 catId为0 不根据catId查询属性组信息
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        //获取关键字
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            //拼接查询的条件
            queryWrapper.and((obj) ->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }


        if(catId == 0){
            //不根据catId查询属性组信息
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
        //根据列表编号来查询
        queryWrapper.eq("catelog_id",catId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 根据三级分类id获取对应的属性组和属性组的属性信息
     * @param cateLogId 分类id
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(Long cateLogId) {
        //1.根据三级分类id获取对应的属性组
        return this.list(
                new QueryWrapper<AttrGroupEntity>()
                        .eq("catelog_id", cateLogId)
        ).stream().map((group) ->{
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrGroupWithAttrsVo);
            //2.根据属性组查到对应的属性组的属性信息
            List<AttrEntity> relationAttr = attrService.getRelationAttr(group.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SpuItemGroupAttrVo> getAttrGroupWithSpuId(Long spuId, Long catalogId) {
        this.baseMapper.getAttrGroupWithSpuId(spuId,catalogId);
        return null;
    }
}
